package com.tsingteng.cosfun.ui.cosfun.adapter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.pili.pldroid.player.widget.PLVideoTextureView;
import com.pili.pldroid.player.widget.PLVideoView;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.bean.RecommentBean;
import com.tsingteng.cosfun.helper.DialogHelper;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverListener;
import com.tsingteng.cosfun.ui.cosfun.cosinterface.ObserverManager;
import com.tsingteng.cosfun.ui.costar.CostarActivity;
import com.tsingteng.cosfun.utils.DataUtil;
import com.tsingteng.cosfun.utils.FileDownUtils;
import com.tsingteng.cosfun.utils.ToastUtils;
import com.tsingteng.cosfun.utils.VideoUtils;
import com.tsingteng.cosfun.widget.zan.LikeView;

import java.util.ArrayList;
import java.util.List;

public class PlayAdapter extends RecyclerView.Adapter<PlayAdapter.ViewHolder>{
    Activity mContext = null;
    ArrayList<RecommentBean> videoList = null;

    private List<String> url = new ArrayList<>();
    private ArrayList<String> urlPath = new ArrayList<>();
    private final Dialog loadingDialog;

    public PlayAdapter(Activity mContext) {
        this.mContext = mContext;
        videoList = new ArrayList<>();
//        videoList = DataUtil.getVideoListData();
        loadingDialog = DialogHelper.getLoadingDialog(mContext);
    }


    @Override
    public PlayAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.play_item_view, parent, false);
        return new PlayAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(PlayAdapter.ViewHolder holder, int position) {
        holder.videoMian.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_FIT_PARENT);
        holder.videoSecond.setDisplayAspectRatio(PLVideoView.ASPECT_RATIO_FIT_PARENT);
        holder.videoMian.setVideoPath(videoList.get(position).getUrl());
        holder.videoSecond.setVideoPath(videoList.get(position).getUrl());
        ImageView imageView = new ImageView(mContext);
        Glide.with(mContext).load(videoList.get(position).getPic()).into(imageView);
        holder.videoMian.setCoverView(imageView);

        ImageView imageView2 = new ImageView(mContext);
        Glide.with(mContext).load(videoList.get(position).getPic()).into(imageView2);
        holder.videoSecond.setCoverView(imageView2);
        if (position == 0) {
//            holder.videoMian.start();
//            holder.videoSecond.start();
        }

        holder.rl_awkward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //下载fil
                url.add("http://zhuangdan.oss-cn-beijing.aliyuncs.com/app/video/20180509/7d88dc6d235ad8824d3887854087a850e267a8ea.mp4");
                url.add("http://zhuangdan.oss-cn-beijing.aliyuncs.com/video/template/clips/clip_6e60829c-387e-4da7-b0f8-0d2df5cc2ed7.mp4");
                url.add("http://zhuangdan.oss-cn-beijing.aliyuncs.com/app/video/20180509/c960e30590f339c3be7e23b98a5307305c22e1f0.mp4");
                for (int i = 0; i < url.size(); i++) {
                    urlPath.add(VideoUtils.VIDEO_DOWN_FILE + VideoUtils.getIntance().splitVideoName(url.get(i)));
                }
                loadingDialog.show();
//                FileDownUtils.getIntance().startMoreDown(mContext, url, urlPath);

            }
        });

        holder.likeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!holder.likeView.isChecked()) {
                    ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_ZAN_ADD, position, 0);
                } else {
                    ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_ZAN_MINUS, position, 0);
                }

//
            }
        });
        holder.tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_COMM, position, 0);
            }
        });
        holder.tv_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_SHARE, position, 0);
            }
        });
        holder.rl_awkward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ObserverManager.getInstance().notifyObserver(ObserverListener.EVENT_AWKWARD, position, 0);
            }
        });

    }

    @Override
    public int getItemCount() {
        if (videoList == null) {
            return 0;
        }
        return videoList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView video_cover;
        PLVideoTextureView videoMian;
        PLVideoTextureView videoSecond;
        ImageView img_play;
        RelativeLayout rootView;
        LikeView likeView;
        TextView tv_likeCount;
        TextView tv_comment;
        TextView tv_share;
        RelativeLayout rl_awkward;

        public ViewHolder(View itemView) {
            super(itemView);
            video_cover = itemView.findViewById(R.id.play_item_view_video_cover);
            videoMian = itemView.findViewById(R.id.play_item_view_video_main);
            videoSecond = itemView.findViewById(R.id.play_item_view_video_second);
            img_play = itemView.findViewById(R.id.play_item_view_play);
            rootView = itemView.findViewById(R.id.play_item_view_root_view);
            likeView = itemView.findViewById(R.id.play_item_view_like);
            tv_likeCount = itemView.findViewById(R.id.play_item_view_likecount);
            tv_comment = itemView.findViewById(R.id.play_item_view_comment);
            tv_share = itemView.findViewById(R.id.play_item_view_share);
            rl_awkward = itemView.findViewById(R.id.play_item_view_awkward_rel);
        }
    }

//    @Override
//    public void onComplete() {
//        loadingDialog.dismiss();
//        Intent intent = new Intent(mContext, CostarActivity.class);
//        intent.putExtra(VideoUtils.VIDEO_MODE_SHOOT, 2);
//        intent.putStringArrayListExtra("videoList", urlPath);
//        mContext.startActivity(intent);
////        new VideoSliceSelectDialog(mContext,urlPath).show();
//        ToastUtils.showToast("下载完成");
//    }
}
